<template>
  <el-dialog title="角色列表" :visible="isShow" @close="cancel">
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="item in roleList"
        :key="item.id"
        :label="item.id"
      >
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <div style="margin-top:20px" />
    <el-row type="flex" justify="center" align="middle">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="cancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/role'
import { getUserDetail } from '@/api/user'
import { assignRole } from '@/api/employee'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 角色列表
      roleList: [],
      // 选中的角色
      checkList: []
    }
  },
  async mounted() {
    // 获取角色列表
    const { rows } = await getRoleList()
    this.roleList = rows
  },
  methods: {
    // 关闭弹窗
    cancel() {
      // 清空选项
      this.checkList = []
      this.$emit('btnCancel')
    },
    // 确认
    async btnOK() {
      // 发请求
      await assignRole({ id: this.userId, roleIds: this.checkList })
      // 提示
      this.$message.success('保存成功')
      // 关闭弹窗
      this.$emit('btnCancel')
    },
    // 根据id获取员工角色详情
    async getRoleDetailById(id) {
      const { roleIds } = await getUserDetail(id)
      this.checkList = roleIds
    }
  }
}
</script>

<style>

</style>
